import React from 'react'
import { Row, Col, Form, TreeSelect, Select, DatePicker, message, Input } from 'antd'
import PropTypes from 'prop-types'
import { CaseContext } from '../context'
import style from './block.module.less'
import FeeShow from './FeeShow'
import { getTagByAction } from '../ajax/caseAjax'
import { awaitWrap } from '@js/tool'
// import { beforeClosedStatusList } from '@js/config'
const { Item } = Form
const { Option } = Select

// 案由改变，获取对应案件类型
async function changeCauseActionHandle (causeaction, ref, callback) {
  if (!causeaction) {
    return callback()
  }
  const hide = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getTagByAction(causeaction))
  const c = ref.current
  hide()
  if (e) {
    return message.warning(e.message)
  }
  if (!c) {
    return false
  }
  const o = {}
  if (!d.tag_casetype) {
    o.casetype = ['一般案件']
  } else {
    o.casetype = [d.tag_casetype, d.tag_casesubtype]
  }
  c.setFieldsValue(o)
  callback()
}

function Main ({ isModify, caseFormRef, forzen }) {
  // const columnMap = { label: 'title' }
  return (
    <CaseContext.Consumer>
      {({ typeTree, causeActionTree, dictionaries, feeType, isModify, disabled, isPre, status, computeFee, changeFeeTypeByCaseType, statisticDisputeAmount, changeProcedure, changeFeeType }) => {
        const procedureList = dictionaries['仲裁程序'] || []
        // const caseSourceList = dictionaries['案件来源'] || []
        const foreignRelatedTypes = dictionaries['案件涉外类型'] || []
        const acceptTypes = dictionaries['受理案件类型'] || []
        // const isModifyBeforeClosed = beforeClosedStatusList.indexOf(status) !== -1 // 结案之前的编辑案件
        function afterChangeCaseType () {
          changeFeeTypeByCaseType()
          statisticDisputeAmount()
          changeProcedure()
          changeFeeType()
          computeFee()
        }
        function afterChangeProducure () {
          changeFeeType()
          computeFee()
        }
        return (
          <>
            <Row>
              <Col span={4} className={style['block-title']}><span>案件基本信息</span></Col>
              <Col span={2} />
              <Col span={8}>
                <Item label='案由' name='cause_action' rules={[{ required: true, message: '案由不能为空' }]}>
                  <TreeSelect
                    style={{ width: '100%' }}
                    dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                    treeData={causeActionTree}
                    placeholder='请选择'
                    disabled={['已归档'].includes(status)}
                    showSearch
                    allowClear
                    onChange={(d) => changeCauseActionHandle(d, caseFormRef, afterChangeCaseType)}
                  />
                </Item>
              </Col>
            </Row>
            {/* {
              !isPre && status ? (
                <Row>
                  <Col span={6} />
                  <Col span={8}>
                    <Item label='扩展案由' name='cause_action_ext'>
                      <TreeSelect
                        style={{ width: '100%' }}
                        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                        treeData={causeActionTree}
                        placeholder='请选择'
                        disabled={!isModifyBeforeClosed}
                        showSearch
                        allowClear
                      />
                    </Item>
                  </Col>
                </Row>
              ) : null
            } */}
            <Row>
              <Col span={6} />
              <Item name='casetype' noStyle><span /></Item>
              {/* <Col span={8}>
                <Item name='casetype' label='案件类别' rules={[{ required: true, message: '案件类别不能为空' }]}>
                  <Cascader options={typeTree} fieldNames={columnMap} disabled={disabled || isModify} allowClear onChange={() => afterChangeCaseType()} />
                </Item>
              </Col> */}
            </Row>
            {/* <Row>
              <Col span={6} />
              <Col span={8}>
                <Item name={['claim', 'fee_type']} label='收费方式'>
                  <Select placeholder='请选择' disabled={disabled || isModify} allowClear onChange={v => computeFee()}>
                    {feeType.map(li => <Option key={li} value={li}>{li}</Option>)}
                  </Select>
                </Item>
              </Col>
            </Row> */}
            <FeeShow caseFormRef={caseFormRef} />
            {!isPre ? (
              <Row>
                <Col span={6} />
                <Col span={8}>
                  <Item label='申请时间' name='accept_datetime' rules={[{ required: true, message: '申请时间不能为空' }]}>
                    <DatePicker style={{ width: '100%' }} disabled={forzen || disabled || isModify} allowClear placeholder='请选择' />
                  </Item>
                </Col>
              </Row>
            ) : null}
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='仲裁程序' name='procedure' rules={[{ required: true, message: '仲裁程序不能为空' }]}>
                  <Select style={{ width: '100%' }} disabled={forzen || disabled || isModify} placeholder='请选择' allowClear onChange={() => afterChangeProducure()}>
                    {procedureList.map(li => (
                      <Option key={li.title} value={li.title}>{li.title}</Option>
                    ))}
                  </Select>
                </Item>
              </Col>
            </Row>
            {/* {!isPre ? (
              <Row>
                <Col span={6} />
                <Col span={8}>
                  <Item label='案件来源' name='accept_structure' rules={[{ required: true, message: '案件来源不能为空' }]}>
                    <Select style={{ width: '100%' }} disabled={disabled} placeholder='请选择' allowClear>
                      {caseSourceList.map(li => (
                        <Option key={li.title} value={li.title}>{li.title}</Option>
                      ))}
                    </Select>
                  </Item>
                </Col>
              </Row>
            ) : null} */}
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='案件涉外类型' name='foreign_related_type'>
                  <Select style={{ width: '100%' }} placeholder='请选择' disabled={forzen} allowClear>
                    {foreignRelatedTypes.map(li => (
                      <Option key={li.title} value={li.value}>{li.title}</Option>
                    ))}
                  </Select>
                </Item>
              </Col>
            </Row>
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='受理案件类型' name='accept_report_type' rules={[{ required: true, message: '受理案件类型不能为空' }]}>
                  <Select style={{ width: '100%' }} placeholder='请选择' disabled={forzen} allowClear>
                    {acceptTypes.map(li => (
                      <Option key={li.title} value={li.value}>{li.title}</Option>
                    ))}
                  </Select>
                </Item>
              </Col>
            </Row>
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='案件标签' name='user_tag'>
                  <Input placeholder='请输入' allowClear disabled={forzen} />
                </Item>
              </Col>
            </Row>
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='仲裁依据' name='basis' rules={[{ required: true, message: '仲裁依据不能为空' }]}>
                  <Input placeholder='请输入' allowClear disabled={forzen} />
                </Item>
              </Col>
            </Row>
            <Row>
              <Col span={6} />
              <Col span={8}>
                <Item label='合同签订时间' name='basis_signed_datetime'>
                  <DatePicker style={{ width: '100%' }} allowClear placeholder='请选择' disabled={forzen} />
                </Item>
              </Col>
            </Row>
          </>
        )
      }}
    </CaseContext.Consumer>
  )
}

Main.propTypes = {
  caseFormRef: PropTypes.object,
  forzen: PropTypes.bool
}

Main.defaultProps = {
  caseFormRef: null,
  forzen: false
}

export default Main
